
*** Table H.12: Spillover

use "$datadir\ewallet\merchants_reshape_week.dta" , clear 
	
	*** Keep only valid pincodes (6 digits)
	tostring pincode, gen(pincode_str)
	g x = length(pincode_str)
	keep if x == 6 
	drop x pincode_str
	
	** Week 28 is the week of the shock
	g onplatform = transaction > 0
	
	egen merchant_id = group(merchantid)
	egen ziptime = group(pincode time)
	egen industrytime = group(industryid time)
	egen zip = group(pincode)
	
	
	bys industryid zip time : egen amount_jt = total(amount) 
	bys industryid zip time : egen transaction_jt = total(transaction) 
	bys industryid zip time : egen onplatform_jt = total(onplatform) 
	
	xtset merchant_id time 
	
	foreach var in amount transaction onplatform{
		g lag_`var'_jt = l.`var'_jt
		g lag_`var' = l.`var'
		g lag_`var'_jt_1 = lag_`var'_jt - lag_`var'
		
		g ln_lag_`var' = ln(1+lag_`var')
		g ln_lag_`var'_jt_1 = ln(1+lag_`var'_jt_1)
		g ln_`var' = ln(1+`var')
	}
	
	
	foreach var in ln_lag_amount  ln_lag_amount_jt_1 ln_amount ln_lag_transaction  ln_lag_transaction_jt_1 ln_transaction {
	bys time: egen m_`var'=mean(`var')
	bys time: egen sd_`var'=sd(`var')
	gen `var'_z=(`var'-m_`var')/sd_`var'
	drop m_`var' sd_`var'
	sum `var'_z
	}

	rename ln_lag_amount_jt_1_z ln_lag_amount_z_jt_1
	rename ln_lag_transaction_jt_1_z  ln_lag_transaction_z_jt_1

	
	*** amount
	foreach var in  amount {
		reg ln_`var' ln_lag_`var' c.ln_lag_`var'_jt_1 , cluster(zip)
		outreg2 using "$outputdir_tab\Table_H_12_1.tex", dec(3) replace
		reghdfe ln_`var' ln_lag_`var' c.ln_lag_`var'_jt_1 , a(merchant_id ) cluster(zip)
		outreg2 using "$outputdir_tab\Table_H_12_1.tex", dec(3) append
		reghdfe ln_`var' ln_lag_`var' c.ln_lag_`var'_jt_1, a(merchant_id ziptime) cluster(zip)
		outreg2 using "$outputdir_tab\Table_H_12_1.tex", dec(3) append
		reghdfe ln_`var' ln_lag_`var' c.ln_lag_`var'_jt_1, a(merchant_id ziptime industrytime) cluster(zip)
		outreg2 using "$outputdir_tab\Table_H_12_1.tex", dec(3) append
	}
	
	*** transactions
	foreach var in  transaction {
		reg ln_`var' ln_lag_`var' c.ln_lag_`var'_jt_1 , cluster(zip)
		outreg2 using "$outputdir_tab\Table_H_12_2.tex", dec(3) replace
		reghdfe ln_`var' ln_lag_`var' c.ln_lag_`var'_jt_1 , a(merchant_id ) cluster(zip)
		outreg2 using "$outputdir_tab\Table_H_12_2.tex", dec(3) append
		reghdfe ln_`var' ln_lag_`var' c.ln_lag_`var'_jt_1 , a(merchant_id ziptime) cluster(zip)
		outreg2 using "$outputdir_tab\Table_H_12_2.tex", dec(3) append
		reghdfe ln_`var' ln_lag_`var' c.ln_lag_`var'_jt_1, a(merchant_id ziptime industrytime) cluster(zip)
		outreg2 using "$outputdir_tab\Table_H_12_2.tex", dec(3) append
	}
	
	*** On platform
	foreach var in  onplatform {
		reg `var' lag_`var' c.ln_lag_`var'_jt_1 , cluster(zip)
		outreg2 using "$outputdir_tab\Table_H_12_3.tex", dec(3) replace
		reghdfe `var' lag_`var' c.ln_lag_`var'_jt_1 , a(merchant_id ) cluster(zip)
		outreg2 using "$outputdir_tab\Table_H_12_3.tex", dec(3) append
		reghdfe `var' lag_`var' c.ln_lag_`var'_jt_1 , a(merchant_id ziptime) cluster(zip)
		outreg2 using "$outputdir_tab\Table_H_12_3.tex", dec(3) append
		reghdfe `var' lag_`var' c.ln_lag_`var'_jt_1 , a(merchant_id ziptime industrytime) cluster(zip)
		outreg2 using "$outputdir_tab\Table_H_12_3.tex", dec(3) append
	}
	 